﻿#include <iostream>
#include <string>
#include <fstream>  
using namespace std;

// 定义链表
class StudentNode {
private:
    string sno;
    string name;
    string sex;
    int age;
    string major;
    StudentNode* next;
    friend class StudentList;
};

// 定义链表管理类
class StudentList {
private:
    StudentNode* head;
    int length;

public:
    StudentList() {
        head = NULL;
        length = 0;
    }

    ~StudentList() {
        StudentNode* temp;
        while (head != NULL) {
            temp = head;
            head = head->next;
            delete temp;
        }
    }

    void init() {
        insert("2023010101", "李雷", "男", 20, "计算机科学与技术");
        insert("2023010201", "李梅", "女", 21, "软件工程");
        insert("2023010301", "张宁", "男", 20, "电子商务");
        insert("2023010102", "王刚", "男", 19, "计算机科学与技术");
        insert("2023020101", "云峰", "男", 19, "通信工程");
        insert("2023020201", "刘雯", "女", 18, "信号与信息系统");
        insert("2023030101", "孙伟", "男", 20, "会计学");
        insert("2023030201", "李欣", "女", 20, "税收学");
        insert("2023040201", "刘海", "男", 20, "英语");
        insert("2023050301", "赵静", "女", 20, "物联网工程");
    }

    void insert(string sno, string name, string sex, int age, string major) {
        StudentNode* newNode = new StudentNode;
        newNode->sno = sno;
        newNode->name = name;
        newNode->sex = sex;
        newNode->age = age;
        newNode->major = major;
        newNode->next = NULL;

        if (head == NULL) {
            head = newNode;
        }
        else {
            StudentNode* temp = head;
            while (temp->next != NULL) {
                temp = temp->next;
            }
            temp->next = newNode;
        }
        length++;
    }

   
    void exportToTxt(string fileName = "StudentInfo.txt") {
        // 判断空
        if (head == NULL) {
            cout << "无学生信息可导出，无法生成记事本！\n";
            return;
        }

        
        ofstream outFile(fileName, ios::out);
        if (!outFile.is_open()) {  
            cout << "记事本创建失败！可能是权限不足或路径错误。\n";
            return;
        }

        // 写入文件内容
        outFile << "学生信息表\n";
        outFile << "-----------------------------------------\n";
        outFile << "学号\t\t姓名\t性别\t年龄\t专业\n";
        outFile << "-----------------------------------------\n";

        StudentNode* temp = head;
        while (temp != NULL) {
           
            outFile << temp->sno << "\t"
                << temp->name << "\t"
                << temp->sex << "\t"
                << temp->age << "\t"
                << temp->major << "\n";
            temp = temp->next;
        }

        
        outFile.close();
        cout << "记事本生成成功！文件路径：" << fileName << "\n";
        cout << "（注：文件默认保存在程序运行目录下）\n";
    }

    
    void search() {
        string sno, name, sex, major;
        int age, option;
        bool found = false;
        StudentNode* temp = head;

        cout << "请输入要查询的字段代码：1-学号，2-姓名，3-性别，4-年龄，5-专业，6-查询全部，0-返回\n";
        cin >> option;
        while (cin.fail()) {
            cin.clear();
            cin.ignore(1024, '\n');
            cout << "输入类型错误，请重新输入...\n";
            cin >> option;
        }

        switch (option) {
        case 1:
            cout << "请输入要查询的学号：\n";
            cin >> sno;
            temp = head;
            while (temp != NULL) {
                if (temp->sno == sno) {
                    printNode(temp);
                    found = true;
                }
                temp = temp->next;
            }
            break;
        case 2:
            cout << "请输入要查询的姓名：\n";
            cin >> name;
            temp = head;
            while (temp != NULL) {
                if (temp->name == name) {
                    printNode(temp);
                    found = true;
                }
                temp = temp->next;
            }
            break;
        case 3:
            cout << "请输入要查询的性别（男/女）：\n";
            cin >> sex;
            temp = head;
            while (temp != NULL) {
                if (temp->sex == sex) {
                    printNode(temp);
                    found = true;
                }
                temp = temp->next;
            }
            break;
        case 4:
            cout << "请输入要查询的年龄：\n";
            cin >> age;
            temp = head;
            while (temp != NULL) {
                if (temp->age == age) {
                    printNode(temp);
                    found = true;
                }
                temp = temp->next;
            }
            break;
        case 5:
            cout << "请输入要查询的专业：\n";
            cin >> major;
            temp = head;
            while (temp != NULL) {
                if (temp->major == major) {
                    printNode(temp);
                    found = true;
                }
                temp = temp->next;
            }
            break;
        case 6:
            printAll();
            found = true;
            break;
        case 0:
            return;
        default:
            cout << "代码错误，请从0-6中选择...\n";
            return;
        }

        if (!found) cout << "查询失败，未找到匹配学生\n";
    }

    void update() {
        string sno, newName, newSex, newMajor;
        int newAge;
        bool found = false;
        StudentNode* temp = head;

        cout << "请输入要修改信息的学生学号：\n";
        cin >> sno;

        while (temp != NULL) {
            if (temp->sno == sno) {
                cout << "请输入新姓名：\n";
                cin >> newName;
                cout << "请输入新性别（男/女）：\n";
                cin >> newSex;
                cout << "请输入新年龄：\n";
                cin >> newAge;
                cout << "请输入新专业：\n";
                cin >> newMajor;

                temp->name = newName;
                temp->sex = newSex;
                temp->age = newAge;
                temp->major = newMajor;
                found = true;
                cout << "学生信息修改成功！\n";
                break;
            }
            temp = temp->next;
        }

        if (!found) cout << "修改失败，未找到该学号的学生\n";
    }

    void remove() {
        string sno;
        bool found = false;
        StudentNode* temp = head, * prev = NULL;

        cout << "请输入要删除学生的学号：\n";
        cin >> sno;

        while (temp != NULL) {
            if (temp->sno == sno) {
                found = true;
                if (prev == NULL) {
                    head = temp->next;
                }
                else {
                    prev->next = temp->next;
                }
                delete temp;
                length--;
                cout << "学生信息删除成功！\n";
                break;
            }
            prev = temp;
            temp = temp->next;
        }

        if (!found) cout << "删除失败，未找到该学号的学生\n";
    }

    void printNode(StudentNode* node) {
        cout << "|学号\t\t|姓名\t|性别\t|年龄\t|专业\n";
        cout << "|" << node->sno << "\t|" << node->name << "\t|"
            << node->sex << "\t|" << node->age << "\t|" << node->major << "\n\n";
    }

    void printAll() {
        if (head == NULL) {
            cout << "当前无学生信息\n";
            return;
        }

        cout << "以下为所有学生信息：\n";
        cout << "--------------------------------------------------------------\n";
        cout << "|序号\t|学号\t\t|姓名\t|性别\t|年龄\t|专业\n";
        cout << "--------------------------------------------------------------\n";
        StudentNode* temp = head;
        int index = 1;
        while (temp != NULL) {
            cout << "|" << index++ << "\t|" << temp->sno << "\t|" << temp->name << "\t|"
                << temp->sex << "\t|" << temp->age << "\t|" << temp->major << "\n";
            temp = temp->next;
        }
        cout << "\n";
    }
};

// 用户
struct User {
    string username;
    string password;
    bool role;
};
User users[3] = {
    {"gaojing","111",1},
    {"yangdongwang","222",0},
    {"qiaobocheng","333",0}
};
int userNumber = 3;

// 菜单函数
void showMenu1();
void showMenu0();

int main() {
    StudentList stuList;
    stuList.init();

    // 登录逻辑
    string username, password;
    bool role, state = true, found = false;
    cout << "欢迎访问学生信息管理系统，请登录...\n";

    while (state) {
        cout << "用户名：";
        cin >> username;
        cout << "密码：";
        cin >> password;

        for (int i = 0; i < userNumber; i++) {
            if (username == users[i].username && password == users[i].password) {
                role = users[i].role;
                state = false;
                found = true;
                cout << "欢迎" << username << "!   您的权限为："
                    << (role ? "读写" : "只读") << "\n\n";
                break;
            }
        }
        if (!found) cout << "登录失败，用户名或密码错误，请重试...\n\n";
    }

    // 权限控制逻辑
    if (role) {
        bool isQuit = false;
        int option;
        while (!isQuit) {
            showMenu1();
            cout << "请选择操作：";
            cin >> option;
            while (cin.fail()) {
                cin.clear();
                cin.ignore(1024, '\n');
                cout << "输入类型错误，请重新输入：";
                cin >> option;
            }

            switch (option) {
            case 0: isQuit = true; break;
            case 1: stuList.search(); break;
            case 2: {
                string sno, name, sex, major;
                int age;
                cout << "请依次输入学生信息：\n";
                cout << "学号："; cin >> sno;
                cout << "姓名："; cin >> name;
                cout << "性别："; cin >> sex;
                cout << "年龄："; cin >> age;
                cout << "专业："; cin >> major;
                stuList.insert(sno, name, sex, age, major);
                cout << "学生信息添加成功！可通过【5.导出到记事本】生成TXT文件\n";  
                break;
            }
            case 3: stuList.update(); break;
            case 4: stuList.remove(); break;
            case 5: stuList.exportToTxt(); break;  
            default: cout << "输入错误，请重试...\n"; break;
            }
        }
    }
    else {
        bool isQuit = false;
        int option;
        while (!isQuit) {
            showMenu0();
            cout << "请选择操作：";
            cin >> option;
            while (cin.fail()) {
                cin.clear();
                cin.ignore(1024, '\n');
                cout << "输入类型错误，请重新输入：";
                cin >> option;
            }

            switch (option) {
            case 0: isQuit = true; break;
            case 1: stuList.search(); break;
            default: cout << "输入错误，请重试...\n"; break;
            }
        }
    }

    system("pause");
    return 0;
}

// 菜单函数实现
void showMenu1() {
    cout << "\n           学生信息管理系统           \n";
    cout << "--------------------------------------\n";
    cout << "************1.学生信息查询************\n";
    cout << "************2.学生信息添加************\n";
    cout << "************3.学生信息修改************\n";
    cout << "************4.学生信息删除************\n";
    cout << "************5.导出到记事本************\n"; 
    cout << "************0.退        出************\n\n";
}

void showMenu0() {
    cout << "\n           学生信息管理系统           \n";
    cout << "--------------------------------------\n";
    cout << "************1.学生信息查询************\n";
    cout << "************0.退        出************\n\n";
}